package cn.tang.tframe.config;

import cn.tang.tframe.common.utils.io.PropertiesUtil;
import cn.tang.tframe.dynconfig.DynConfigRegister;
import cn.tang.tframe.dynconfig.IDynConfigProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;


/**
 * Created by tanghc
 *
 */
public class QtMonitorConfig {

	public static final Logger logger = LoggerFactory.getLogger(QtMonitorConfig.class);

	private QtMonitorConfig(){}

	private static String config_group = "common";
	private static String config_data_id = "monitor";
	private static String config_mybatis_on = "mybatisOn";
	private static String config_mybatis_rate = "mybatisRate";
	private static String config_dubbo_on = "dubboOn";
	private static String config_dubbo_rate = "dubboRate";
	private static String config_redis_on = "redisOn";
	private static String config_redis_rate = "redisRate";

	public static boolean mybatis_on = Boolean.FALSE;
	public static boolean dubbo_on = Boolean.FALSE;
	public static boolean redis_on = Boolean.FALSE;
	public static int mybatis_rate = 0;
	public static int dubbo_rate = 0;
	public static int redis_rate = 0;
	static {
		DynConfigRegister.getInstance().buildOnce(config_group, config_data_id, new IDynConfigProcessor() {
			@Override
			public void procesConfig(String config) {
				Properties properties = PropertiesUtil.loadFromString(config);
				if(null != properties){
					Object mybatisOn = properties.get(config_mybatis_on);
					if(null != mybatisOn ){
						String s = mybatisOn.toString();
						if(s.equals("true") || s.equals("1") || s.equals("on")){
							mybatis_on = true;
						}
					}
					Object mybatisRate = properties.get(config_mybatis_rate);
					if(null != mybatisRate ){
						String s = mybatisOn.toString();
						int tmp = -1;
						try {
							tmp = Integer.parseInt(s);
							if(tmp < 0 || tmp > 100){
								throw new RuntimeException("rate must between 0~100");
							}
							mybatis_rate = tmp;
						} catch (Exception e) {
							logger.warn("invalid integer value: key={}",config_mybatis_rate);
						}
					}
					Object dubboOn = properties.get(config_dubbo_on);
					if(null != dubboOn ){
						String s = dubboOn.toString();
						if(s.equals("true") || s.equals("1") || s.equals("on")){
							dubbo_on = true;
						}
					}
					Object dubboRate = properties.get(config_dubbo_rate);
					if(null != dubboRate ){
						String s = dubboOn.toString();
						int tmp = -1;
						try {
							tmp = Integer.parseInt(s);
							if(tmp < 0 || tmp > 100){
								throw new RuntimeException("rate must between 0~100");
							}
							dubbo_rate = tmp;
						} catch (Exception e) {
							logger.warn("invalid integer value: key={}",config_dubbo_rate);
						}
					}
					Object redisOn = properties.get(config_redis_on);
					if(null != redisOn ){
						String s = redisOn.toString();
						if(s.equals("true") || s.equals("1") || s.equals("on")){
							redis_on = true;
						}
					}
					Object redisRate = properties.get(config_redis_rate);
					if(null != redisRate ){
						String s = redisOn.toString();
						int tmp = -1;
						try {
							tmp = Integer.parseInt(s);
							if(tmp < 0 || tmp > 100){
								throw new RuntimeException("rate must between 0~100");
							}
							redis_rate = tmp;
						} catch (Exception e) {
							logger.warn("invalid integer value: key={}",config_redis_rate);
						}
					}
				}
			}
		});
	}
}
